# Copyright 2012 Brett Ponsler
# This file is part of siriviewer.
#
# siriviewer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# siriviewer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with siriviewer.  If not, see <http://www.gnu.org/licenses/>.
''' '''
from os.path import join

from PyQt4.Qt import SIGNAL
from PyQt4 import QtGui, Qt, QtCore
from PyQt4.QtGui import QSizePolicy

from siriviewer.objects import AddViews
from siriviewer.objects import Factory

from pyamp.ui.qt import ScrollableList, StyleSheet


class ObjectView(ScrollableList):
    ''' '''
    size = QtCore.QSize(400, 600)

    # @todo: use config file to get this directory
    __ImageDir = join("/home", "bponsler", "personal", "projects", "python",
                      "siriviewer", "images")
    __TopImage = join(__ImageDir, "siri_top.png")
    __MiddleImage = join(__ImageDir, "siri_middle.png")
    __BottomLeftImage = join(__ImageDir, "siri_bottom_left.png")
    __BottomRightImage = join(__ImageDir, "siri_bottom_right.png")

    def addRequestObject(self, request):
        '''Add an object for a Siri request.

        * request -- The requested text

        '''
        requestObj = {
            'refId': 'TEST REFID',
            'aceId': '54902246-9bb2-49e3-8895-2ab439fc6e53',
            'group': 'com.apple.ace.assistant',
            'class': 'AddViews',
            'properties': {
                'scrollToTop': False,
                'temporary': False,
                'dialogPhase': 'Completion',
                'views': [{
                        'group': 'com.apple.ace.assistant',
                        'class': 'AssistantUtteranceView',
                        'properties': {
                            'text': request,
                            'listenAfterSpeaking': False,
                            'speakableText': request,
                            'dialogIdentifier': 'Misc#ident'}}]}}

        self.__addObject(requestObj, self.__BottomRightImage)

    def addResponseObject(self, obj):
        '''Add an object for a Siri response.

        * obj -- The response object

        '''
        self.__addObject(obj, self.__BottomLeftImage)

        # @todo: add method to find position of widget within the scrollable
        # @todo: list will need to add same method to WidgetList...
        # @todo: use that method to determine where it is within the scroll area
        # @todo: and add a spacer to the bottom to (?) to force it to the top
        # @todo: of the scroll area...

        # name = "Top-4"
        # maxHeight = maxY = 0
        # for widget in self.getWidgetList().getWidgets():
        #     if str(widget.objectName()) == name:
        #         if widget.y() > maxY:
        #             maxY = widget.y()
        #             maxHeight = widget.height

        # # @todo: this is close but not perfect
        # print "********** MAX Y:", maxY
        # self.scrollTo(maxY)

    def __addObject(self, obj, bottomImage):
        '''Add a specific object to the view.

        * obj -- The object to add to the view
        * bottomImage -- The image to display at the bottom of the object

        '''
        widget = Factory.create(self.controller, obj, self.width() - 100,
                                self.height())
        if widget is not None:
            # Add the top image for the quotation
            self.__addImage("Top", self.__TopImage)

            self.__styleWidget(widget)
            self.addWidget(widget)

            # Add the bottom image for the quotation
            self.__addImage("Bottom", bottomImage)

        # @todo: for now just scroll to the bottom...
        self.scrollToBottom()

    def __styleWidget(self, widget):
        '''Set the style sheet for the given widget.

        * widget -- The widget

        '''
        ss = StyleSheet(widget="QWidget#%s" % widget.name,
                        border_image='url("%s")' % self.__MiddleImage)
        widget.setStyleSheet(str(ss))

    def __addImage(self, name, filename):
        '''Add a widget containing the given image to the given view.

        * name -- The name of the image widget
        * filename -- The filename for the image

        '''
        imageWidget = QtGui.QWidget()
        imageWidget.setObjectName(name)
        imageWidget.setMinimumSize(self.width(), 12)

        styleSheet = StyleSheet(background_image="url(%s)" % filename)
        imageWidget.setStyleSheet(str(styleSheet))
        self.addWidget(imageWidget)
